package net.jalbum.filters;

import java.awt.image.BufferedImage;
import javax.swing.ImageIcon;

import net.jalbum.filterManager.FilterProperties.FilterCategory;
import se.datadosen.jalbum.Msg;

public class InvertFilterPlugin extends BasicFilter {

	private static final long serialVersionUID = 1L;

	public InvertFilterPlugin() {
		this.name = Msg.getString(this, "filters.invert.name");
		this.shortName = Msg.getString(this, "filters.invert.shortName");
		this.icon = new ImageIcon(this.getClass().getResource("res/invert.png"));
		this.author = "David Fichtmueller";
		this.version = "1.0";
		this.description = Msg.getString(this, "filters.invert.description");
		this.category = FilterCategory.FUN;
		this.prescale = true;
		this.postscale = true;
	}

	/**
	 * We never touch the original image so avoid cloning
	 * @param bi
	 * @return
	 */
	@Override
	protected BufferedImage cloneBufferedImage(final BufferedImage bi) {
		return bi;
	}

	@Override
	public BufferedImage renderImage(final BufferedImage bi) {
		final BufferedImage biOut = new BufferedImage(bi.getWidth(), bi.getHeight(), bi.getType());

		for (int x = 0; x < bi.getWidth(); x++) {
			for (int y = 0; y < bi.getHeight(); y++) {
				int rgb = bi.getRGB(x, y);

				// get the rgb-values
				final int alpha = (int) ((rgb & 0xff000000l) >> 24);
				int r = ((rgb & 0x00ff0000) >> 16);
				int g = ((rgb & 0x0000ff00) >> 8);
				int b = ((rgb & 0x000000ff));

				// Invert values
				r = 255 - r;
				g = 255 - g;
				b = 255 - b;

				rgb = ((alpha & 0xff) << 24) | ((r & 0xff) << 16) | ((g & 0xff) << 8) | (b & 0xff);
				biOut.setRGB(x, y, rgb);
			}
		}
		return biOut;
	}
}
